import aiohttp
import asyncio
import re
from bs4 import BeautifulSoup
import json

async def get_bilibili_video_info(url, headers):
    """获取B站视频信息"""
    async with aiohttp.ClientSession() as session:
        async with session.get(url, headers=headers) as response:
            html = await response.text()
            soup = BeautifulSoup(html, 'html.parser')
            
            # 提取播放信息
            playinfo_match = re.search(r'window\.__playinfo__=(.*?)</script>', html, re.DOTALL)
            if not playinfo_match:
                raise ValueError("未找到播放信息")
            play_data = json.loads(playinfo_match.group(1))['data']['dash']
            
            # 提取统计数据
            stat_classes = {
                'like': 'video-like-info',
                'coin': 'video-coin-info',
                'favorite': 'video-fav-info',
                'share': 'video-share-info'
            }
            stats = {k: soup.find('span', class_=v).text for k, v in stat_classes.items()}
            
            # 处理封面URL
            cover_meta = soup.find('meta', itemprop='image')
            cover_url = f"https:{cover_meta.get('content').split('@')[0]}" if cover_meta else None
            
            return {
                'title': soup.find('title').text,
                'cover': cover_url,
                'video_url': play_data['video'][0]['baseUrl'],
                'audio_url': play_data['audio'][0]['baseUrl'],
                **stats
            }

if __name__ == "__main__":
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0',
        'Referer': 'https://www.bilibili.com/',
        'cookie': "buvid3=93F12A49-F981-A1BA-0AED-15F0D142A79886204infoc; b_nut=1761553986; _uuid=E10F10E877-9EC2-1273-CD94-9D69B1C38D6D87262infoc; enable_web_push=DISABLE; buvid_fp=1fed7b07500755e95e1beb048c9bb0df; buvid4=56A6D30D-9A98-A987-2B51-47CCDC6B257686954-025102716-utYYOJL/XC9Jt+w4cwQwmg%3D%3D; SESSDATA=60eef209%2C1777170984%2C5291e%2Aa2CjA9mXUMkzvODcTYlLOoWzn1kzivtVrpv1umuWM4oQDoh5dzEPcDb4FI9UsEoqGUDfASVk5peV9ISWVvMEtVUENtd2gxRkZfbjNPZ0haLThpbDFSR2RlUXJPVVF6ZXRUMGRZRHo2emRtTWxRWGdKMmRMZDNjZ1RCRjItbVNJa3hRZkxJemlrTVJ3IIEC; bili_jct=897141e18595e2e15d066305bcfbcde7; DedeUserID=214438039; DedeUserID__ckMd5=2ecf106214ccb391; theme-tip-show=SHOWED; rpdid=|(k))k)~~JYl0J'u~YuYkuYYu; theme-avatar-tip-show=SHOWED; home_feed_column=5; CURRENT_QUALITY=80; browser_resolution=1826-962; CURRENT_LANGUAGE=; bp_t_offset_214438039=1133468648535490560; hit-dyn-v2=1; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NjMwMDM4NTIsImlhdCI6MTc2Mjc0NDU5MiwicGx0IjotMX0.S2_iDsQchaqkY7I1kEaxBnYeSdeBE-7pdX3VVxpTha8; bili_ticket_expires=1763003792; ogv_device_support_hdr=0; b_lsid=CE516CE6_19A6BC603E8; bmg_af_switch=1; bmg_src_def_domain=i1.hdslb.com; sid=81p3rxb6; CURRENT_FNVAL=4048"
    }
    
    url = input('请输入B站视频链接: ')
    info = asyncio.run(get_bilibili_video_info(url, headers))
    
    # 统一输出格式
    labels = {
        'title': '视频标题', 'cover': '视频封面', 'video_url': '视频链接',
        'audio_url': '音频链接', 'like': '点赞数', 'coin': '硬币数',
        'favorite': '收藏数', 'share': '分享数'
    }
    for key, label in labels.items():
        print(f"{label}: {info.get(key, 'N/A')}")